﻿using AutoMapper;
using B.R.RabbitRoot.Domain;
using B.R.RabbitRoot.ErrorCode;
using B.R.RabbitRoot.Infrastructure.Interface;
using B.R.RabbitRoot.Write.API.Applications.Commandhandlers.ImgCommandHandlers;
using MediatR;

namespace B.R.RabbitRoot.Write.API.Applications.Commands.ImgCommands
{
    /// <summary>
    /// 图片删除处理
    /// </summary>
    public class ImageTableDeleteCommand : IRequestHandler<ImageTableDeleteCommandHandler, APIResult<int>>
    {
        private readonly IBaserepository<ImageTable> ImageTableredsitoty;
        private readonly IMapper mapper;
        public ImageTableDeleteCommand(IBaserepository<ImageTable> imageTableredsitoty, IMapper mapper)
        {
            ImageTableredsitoty = imageTableredsitoty;
            this.mapper = mapper;
        }

        public async Task<APIResult<int>> Handle(ImageTableDeleteCommandHandler request, CancellationToken cancellationToken)
        {
            APIResult<int> result=new APIResult<int>();
            result.Code=ResultCode.删除成功;
            result.Message = ResultCode.删除成功.ToString();
            var entity = ImageTableredsitoty.GetAll().FirstOrDefault(x => x.SecondaryCategory== request.SecondaryCategory) ;


            var obj = await ImageTableredsitoty.removeAsync(entity);
            result.Data = obj;
            return await Task.FromResult(result);
        }
    }
}
